package com.haobao.telephonerecord

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.haobao.telephonerecord.utils.TimeUtils

/**
 * @author ypk
 * 创建日期：2021/7/23  17:24
 * 描述：
 */
class RecordAdapter constructor(val context: Context, val mList: MutableList<Song>) :
    RecyclerView.Adapter<RecordAdapter.RecordViewHolder>() {

    public fun setNewData(mList1: MutableList<Song>) {
        mList.clear()
        mList.addAll(mList1)
        notifyDataSetChanged()
    }

    inner class RecordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvName: TextView = itemView.findViewById(R.id.tvName)
        var tvPath: TextView = itemView.findViewById(R.id.tvPath)
        var tvDate: TextView = itemView.findViewById(R.id.tvDate)
        var tvUpload: Button = itemView.findViewById(R.id.tvUpload)

        var tvDuration: TextView = itemView.findViewById(R.id.tvDuration)

    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecordAdapter.RecordViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_record, null)
        return RecordViewHolder(view)
    }

    override fun onBindViewHolder(holder: RecordAdapter.RecordViewHolder, position: Int) {
        val bean = mList.get(position)

        holder.tvName.setText(bean.song)
        holder.tvPath.setText(bean.path)

        val duration = bean.duration / 1000
        holder.tvDuration.text = "通话时长：${duration} 秒"

        if (bean.date != null) {
            val toLong = bean.date.toLong() * 1000
            holder.tvDate.setText("日期：" + TimeUtils.getDateToString(toLong))
        } else {
            holder.tvDate.setText("日期： null ")
        }


        holder.tvUpload.setOnClickListener {
            //Toast.makeText(context, "开发中...", Toast.LENGTH_SHORT).show();
            Toast.makeText(context, "开发中...(可加我qq:2908363622)", Toast.LENGTH_SHORT).show();
        }

    }

    override fun getItemCount(): Int {
        return mList.size
    }


}